/*
Copyright 2011 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.gal.progress.desktop;

import java.util.HashMap;
import java.util.UUID;

import javax.swing.JProgressBar;

import nu.zoom.gal.progress.Progress;
import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.desktop.WorkbenchListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * An implementation of the Progress interface. Displays progress in the zoom.nu
 * desktop status bar.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 * 
 */
public class ProgressImpl implements Progress, WorkbenchListener {
	private final Workbench workbench;
	private final Log log = LogFactory.getLog(getClass());
	private JProgressBar progressBar;
	private final HashMap<TaskID, Void> knownTasks = new HashMap<Progress.TaskID, Void>();

	public ProgressImpl(final Workbench workbench) {
		super();
		this.workbench = workbench;
	}

	/**
	 * @author Johan Maasing
	 * 
	 */
	public final class InternalTaskID implements Progress.TaskID {
		private final UUID id;

		public InternalTaskID() {
			this.id = UUID.randomUUID();
		}

		@Override
		public int hashCode() {
			return id.hashCode();
		}

		@Override
		public boolean equals(Object obj) {
			return id.equals(obj);
		}

		@Override
		public String toString() {
			return id.toString();
		}
	}

	@Override
	public TaskID createTask() {
		final TaskID taskID = new InternalTaskID();
		knownTasks.put(taskID, null);
		return taskID;
	}

	@Override
	public void deleteTask(TaskID taskID) {
		if (knownTasks.containsKey(taskID)) {
			log.debug("Removed task ID: " + taskID);
			progressBar.setIndeterminate(false);
			progressBar.setMinimum(0);
			progressBar.setMaximum(100);
			progressBar.setValue(0);
			knownTasks.remove(taskID);
		}
	}

	@Override
	public void setIndeterminate(final TaskID taskID, final boolean value) {
		if (knownTasks.containsKey(taskID)) {
			log.debug("Known TaskID: " + taskID);
			progressBar.setIndeterminate(value);
		}
	}

	@Override
	public void setMaximum(final TaskID taskID, final int max) {
		if (knownTasks.containsKey(taskID)) {
			log.debug("Known TaskID: " + taskID);
			progressBar.setMaximum(max);
		}
	}

	@Override
	public void setValue(final TaskID taskID, final int n) {
		if (knownTasks.containsKey(taskID)) {
			log.debug("Known TaskID: " + taskID);
			progressBar.setValue(n);
		}
	}

	@Override
	public void setMinimum(final TaskID taskID, final int min) {
		if (knownTasks.containsKey(taskID)) {
			log.debug("Known TaskID: " + taskID);
			progressBar.setMinimum(min);
		}
	}

	@Override
	public void setMessage(final TaskID taskID, CharSequence message) {
		if (knownTasks.containsKey(taskID)) {
			log.debug("Known TaskID: " + taskID);
			workbench.getStatusbar().setMessage(message != null ? message.toString() : "");
		}
	}

	@Override
	public void start() {
		progressBar = new JProgressBar();
		// workbench.getStatusbar().addComponent(progressBar);
	}

	@Override
	public void close() {
	}

	@Override
	public void initialize() throws Exception {
		this.workbench.addWorkBenchListener(this);
	}

	@Override
	public void visible() {
	}

}
